package com.yystar.support.util;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
import com.yystar.support.error.JavaScriptErrorReporter;

/**
 * 压缩操作类
 * 
 * @author liuzhifei
 * 
 */
public abstract class MinifierUtility {

    // css line-bread处理
    private static final int _CSS_LINE_BREAK = -1;

    // 禁止优化
    private static final boolean _JS_DISABLE_OPTIMIZATIONS = false;

    // JS line-bread处理
    private static final int _JS_LINE_BREAK = -1;

    // 混淆
    private static final boolean _JS_MUNGE = true;

    // 保留所有的分号
    private static final boolean _JS_PRESERVE_ALL_SEMICOLONS = false;

    private static final boolean _JS_VERBOSE = false;

    private static Log _log = LogFactory.getLog(MinifierUtility.class);

    @Deprecated
    public static String getStaticResourceURL(String uri) {
        return getStaticResourceURL(uri, null);
    }

    @Deprecated
    public static String getStaticResourceURL(String uri, String queryString) {

        if (uri.indexOf('?') != -1) {
            return uri;
        }

        if (uri.startsWith("//")) {
            uri = uri.substring(1);
        }

        StringBuffer sb = new StringBuffer(5);

        // uri
        sb.append(uri);
        sb.append('?');

        // querString
        if (WTextUtility.isNotNull(queryString)) {
            sb.append(queryString);
            sb.append('&');
        }

        // Minifier

        if (uri.endsWith(".css") && WTextUtility.parseBooleanString(SystemProperties.get("css.fast.load"))) {
            sb.append("minifierType=css");
        } else if (uri.endsWith(".js") && WTextUtility.parseBooleanString(SystemProperties.get("js.fast.load"))) {
            sb.append("minifierType=js");
        } else {
            sb.deleteCharAt(sb.length() - 1);
        }

        return sb.toString();
    }

    /**
     * CSS 压缩
     * 
     * @param content
     *            需要压缩的CSS内容
     * @return 压缩后的CSS内容
     */
    public static String minifyCss(String content) {
        Writer stringWriter = new StringWriter();

        try {
            CssCompressor cssCompressor = new CssCompressor(new StringReader(content));

            cssCompressor.compress(stringWriter, _CSS_LINE_BREAK);
        } catch (IOException e) {
            _log.error("CSS Minifier failed for\n" + content);
        }

        return stringWriter.toString();
    }

    /**
     * Java Script 压缩
     * 
     * @param content
     *            需要压缩的JS内容
     * @return 压缩后的JS内容
     */
    public static String minifyJavaScript(String content) {
        Writer stringWriter = new StringWriter();

        try {
            JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor(new StringReader(content), new JavaScriptErrorReporter());

            javaScriptCompressor.compress(stringWriter, _JS_LINE_BREAK, _JS_MUNGE, _JS_VERBOSE, _JS_PRESERVE_ALL_SEMICOLONS, _JS_DISABLE_OPTIMIZATIONS);
        } catch (IOException e) {
            _log.error("JavaScript Minifier failed for\n" + content);
        }

        return stringWriter.toString();
    }
    
}